﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace weighing
{
    /// <summary>
    /// RunMessageUc.xaml 的交互逻辑
    /// </summary>
    public partial class RunMessageUc : System.Windows.Controls.UserControl
    {

        #region varialbes

        public StatusBarData mStatusBarData;

        private Stack<SimpleMessage> mStack;
        private Queue<SimpleMessage> mQueue;

       private DispatcherTimer mTimer;
        #endregion

        public RunMessageUc()
        {
            InitializeComponent();
            if(System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))return;
        }
        #region 控件事件
        private void RunMessageUserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if(mStatusBarData == null)
            {
                mStatusBarData = new StatusBarData();
            }
          
            mTimer = new DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(1)
            };
            mTimer.Tick += MTimer_Tick;

            mStack = new Stack<SimpleMessage>();
            mQueue = new Queue<SimpleMessage>();
        }

        private void MTimer_Tick(object sender, EventArgs e)
        {
          if(mStatusBarData.RunMessages == null)
            {
                mStatusBarData.RunMessages = new List<SimpleMessage>();
            }           
            var sp = new SimpleMessage() { Addtime = DateTime.Now, Level = 0, Description = "MTimer_Tick" };
     
            mStatusBarData.RunMessages.Add(sp);

        }

        private void RunMessageUserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            mTimer.Tick -= MTimer_Tick;          
            mTimer?.Stop();
        }

        private void RunMessageUserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (IsLoaded && IsVisible)
            {
                mTimer?.Start();
            }
            else
            {
                mTimer?.Stop();
            }
        }

        #endregion

    }
}
